package com.cloudcentral.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudcentral.entity.Auth;
import com.cloudcentral.exception.FailureException;
import com.cloudcentral.mapper.AuthMapper;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 系统菜单权限表 服务实现类
 * </p>
 *
 * @author Administrator
 * @since 2023-10-27
 */
@Slf4j
@Service
public class AuthService extends ServiceImpl<AuthMapper, Auth> {
  @Transactional(rollbackFor = {Exception.class})
  @GlobalTransactional(rollbackFor = {Exception.class})
  public Auth submit(Auth auth) throws Exception {
    if (auth.getPlatformId() == null) throw new FailureException("缺少平台ID");
    else if (StringUtils.isBlank(auth.getName())) throw new FailureException("缺少权限名称");
    else if (StringUtils.isBlank(auth.getUrl())) throw new FailureException("缺少权限地址");
    else {
      saveOrUpdate(auth);
      return getById(auth.getId());
    }
  }

  @Transactional(rollbackFor = {Exception.class})
  @GlobalTransactional(rollbackFor = {Exception.class})
  public Boolean delete(Long id) {
    return removeById(id);
  }

  public List<Auth> listByUser(Long platformId, Long menuId, Long userId) {
    return baseMapper.listByUser(platformId, menuId, userId);
  }
}
